﻿Imports Lourdes.BackEnd.BC
Imports Lourdes.BackEnd.Modelo
Imports Lourdes.BackEnd
Public Class frmClienteBuscar
  Dim objPersona As BC_Persona

  Public Event PersonaSeleccionada(ByVal pPersona As Persona)

  Public Sub GetPersonaByDocNum(ByVal pDocNum As String, Optional pParent As Form = Nothing)
    Try

      ' Me.frmClienteBuscar_Load(Me, Nothing)
      Dim lstPersona = objPersona.GetToBuscador("", "", pDocNum, CInt(Enums.PersonaTipo.Cliente))
      If lstPersona.Count = 1 Then
        RaiseEvent PersonaSeleccionada(lstPersona.FirstOrDefault)
      Else
        txtNumeroDoc.Text = pDocNum
        btnFiltrar_Click(Me, Nothing)
        If pParent IsNot Nothing Then
          Me.Show(pParent)
        Else
          Me.Show()
        End If

        If lstPersona.Count > 1 Then
          grdDatos.Focus()
          grdDatos.Rows.FirstOrDefault.Selected = True
        Else
          txtNombre.Focus()
        End If
      End If
    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
    Try
      Dim lstPersona = objPersona.GetToBuscador(txtNombre.Text, txtApellido.Text, _
                                                        txtNumeroDoc.Text, cmbTipoPersona.SelectedValue)
      With grdDatos
        .DataSource = lstPersona
        .DataBind()
        .Focus()
        If lstPersona.Count > 0 Then
          .Rows.FirstOrDefault.Selected = True
        End If
      End With

    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  Private Sub frmClienteBuscar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.Focus()
    TiposCargar()
  End Sub

  Private Sub TiposCargar()
    Dim objTipo As BC_PersonaTipo
    Try
      objTipo = New BC_PersonaTipo
      Dim lstTipos = objTipo.GetAll()

      With cmbTipoPersona
        .DataSource = lstTipos
        .DisplayMember = "Descripcion"
        .ValueMember = "TipoPersonaId"
      End With

    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  Private Sub grdDatos_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles grdDatos.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
      If grdDatos.Selected.Rows.Count > 0 Then
        'MessageBox.Show(grdDatos.Selected.Rows(0).Cells("PersonaId").Value)
        Dim id As Integer = grdDatos.Selected.Rows(0).Cells("PersonaId").Value
        Dim lstPersona As List(Of Persona) = grdDatos.DataSource
        Dim unaPersona As Persona = (From p In lstPersona Where p.PersonaId = id).FirstOrDefault
        'MessageBox.Show(unaPersona.Nombre & " " & unaPersona.Apellido)
        RaiseEvent PersonaSeleccionada(unaPersona)
        Me.Close()
        'Me.Dispose()
      End If
    End If
  End Sub

  Public Sub New()

    ' Llamada necesaria para el diseñador.
    InitializeComponent()

    ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
    objPersona = New BC_Persona
  End Sub
End Class